package com.dhnsoft.education.service.article.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhnsoft.education.entity.article.EduArticle;
import com.dhnsoft.education.entity.article.EduArticleCategory;
import com.dhnsoft.education.entity.article.vo.ArticleReq;
import com.dhnsoft.education.entity.edu.EduTeacher;
import com.dhnsoft.education.mapper.article.EduArticleMapper;
import com.dhnsoft.education.service.article.EduArticleCategoryService;
import com.dhnsoft.education.service.article.EduArticleService;
import com.dhnsoft.education.service.edu.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author zsr
 * @since 2022-01-05
 */
@Service
public class EduArticleServiceImpl extends ServiceImpl<EduArticleMapper, EduArticle> implements EduArticleService {

    @Autowired
    private EduArticleCategoryService articleCategoryService;

    @Autowired
    private EduTeacherService teacherService;

    @Override
    public void pageQuery(Page<EduArticle> page, ArticleReq articleReq) {
        //MP 构建条件
        QueryWrapper<EduArticle> wrapper = new QueryWrapper<>();
        wrapper.select("id", "title", "cover_image", "teacher_id", "category_id", "price", "vote_count", "view_count", "comments_count", "gmt_create", "gmt_modified");
        String title = articleReq.getTitle();
        String begin = articleReq.getBegin();
        String end = articleReq.getEnd();

        if (!StringUtils.isEmpty(title)) wrapper.like("title", title);
        if (!StringUtils.isEmpty(begin)) wrapper.ge("gmt_create", begin);//大于
        if (!StringUtils.isEmpty(end)) wrapper.le("gmt_modified", end);//小于

        //搜索全部分类 转为map
        QueryWrapper<EduArticleCategory> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.select("id", "name");
        Map<String, String> cMaps = articleCategoryService.list(categoryQueryWrapper)
                .stream().collect(Collectors.toMap(EduArticleCategory::getId, EduArticleCategory::getName));

        QueryWrapper<EduTeacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.select("id", "name");
        Map<String, String> tMaps = teacherService.list(teacherQueryWrapper)
                .stream().collect(Collectors.toMap(EduTeacher::getId, EduTeacher::getName));

        baseMapper.selectPage(page, wrapper);
        page.getRecords().forEach(e -> {
            e.setCategoryName(cMaps.get(e.getCategoryId()));
            e.setTeacherName(tMaps.get(e.getTeacherId()));
        });
    }

    @Cacheable(value = "article", key = "'selectIndexList'", cacheManager = "redisCacheManager")
    @Override
    public List<EduArticle> getFourList() {
        QueryWrapper<EduArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "cover_image", "title", "view_count");
        queryWrapper.last("limit 4");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<EduArticle> myQueryPage(long current, long limit, String categoryId) {
        return baseMapper.myQueryPage(current, limit, categoryId);
    }

    @Override
    public void updatePageViewCount(String id) {
        EduArticle article = baseMapper.selectById(id);
        article.setViewCount(article.getViewCount() + 1);
        baseMapper.updateById(article);
    }

    @Override
    public List<EduArticle> getHotArticle() {
        QueryWrapper<EduArticle> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 4");
        List<EduArticle> articleList = baseMapper.selectList(wrapper);
        return articleList;
    }

    @Override
    public void updateBuyCount(String targetId) {
        EduArticle article = baseMapper.selectById(targetId);
        article.setBuyCount(article.getBuyCount() + 1);
        baseMapper.updateById(article);
    }
}
